import BaseBlock from './base'
import blockConf from '../../confs/block-conf'
/**
 * 柱体
 */
export default class Cylinder extends BaseBlock {
  constructor(x, y, z, type, name, width) {
    super('cylinder')
    const size = width || this.width
    
    // const size = this.width
    var geometry = new THREE.CylinderGeometry(size / 2, size / 2, this.height, 120)
    // var material = new THREE.MeshPhongMaterial({
    //   color: 0xffffff
    // })
    // this.instance = new THREE.Mesh(geometry, material)
    // this.instance.receiveShadow = true
    // this.instance.castShadow = true

    if (name == 'color') {
      var currentColor
      const seed = Math.floor(Math.random() * 6)
      switch (seed) {
        case 0:
          currentColor = blockConf.colors.orange
          break
        case 1:
          currentColor = blockConf.colors.orangeDark
          break
        case 2:
          currentColor = blockConf.colors.purple
          break
        case 3:
          currentColor = blockConf.colors.green
          break
        case 4:
          currentColor = blockConf.colors.blue
          break
        case 5:
          currentColor = blockConf.colors.yellow
          break
        default:
      }
      const innerMaterial = new THREE.MeshLambertMaterial({
        color: blockConf.colors.white
      })
      const outerMaterial = new THREE.MeshLambertMaterial({
        color: currentColor
      })

      const innerHeight = 3
      const outerHeight = (blockConf.height - innerHeight) / 2
      const outerGeometry = new THREE.CylinderGeometry(size / 2, size / 2, outerHeight, 120)
      const innerGeometry = new THREE.CylinderGeometry(size / 2, size / 2, innerHeight, 120)


      const totalMesh = new THREE.Object3D()
      const topMesh = new THREE.Mesh(outerGeometry, outerMaterial)
      topMesh.position.y = (innerHeight + outerHeight) / 2
      topMesh.receiveShadow = true
      topMesh.castShadow = true

      const middleMesh = new THREE.Mesh(innerGeometry, innerMaterial)
      middleMesh.receiveShadow = true
      middleMesh.castShadow = true

      const bottomMesh = new THREE.Mesh(innerGeometry, outerMaterial)
      bottomMesh.position.y = -(innerHeight + outerHeight) / 2
      bottomMesh.receiveShadow = true
      bottomMesh.castShadow = true

      totalMesh.add(topMesh)
      totalMesh.add(middleMesh)
      totalMesh.add(bottomMesh)
      this.instance = totalMesh

    }
    
    this.instance.name = 'block'
    this.x = x
    this.y = y
    this.z = z
    
    if (type == 'popup') {
      this.popup()
    } else if (type == 'show') {
      this.instance.position.x = this.x
      this.instance.position.y = this.y
      this.instance.position.z = this.z
    }
  }

}